<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ConfigController extends Controller
{
    //网站配置表页
    public function getIndex()
    {   
    	$data = DB::table('config') -> get();
    	// dd($data);
    	return view('config.index',['data'=>$data]);
    }
    // 网站配置修改页面
    public function getEdit(Request $request)
    {         
        $data = $request->all();
        $id = $data['id'];
        //网站标题
        $title = '网站配置修改';
    	//查询数据
    	$data = DB::table('config') -> where('id',$id) ->first();
        // dd($data);
    	//解析模板
    	return view('config/edit',['data'=>$data,'title'=>$title]);
    }
    //定义后台网站配置的处理修改操作
    public function postUpdate(Request $request)
    {
        // dd($request->all());
       
    	$data = $request -> except('_token','oldlogo');
         $data['logo'] = $this ->upload($request);
         // dd($data);
    	$this -> validate($request,[
    		'title' => 'required',
    		'keywords' => 'required',
    		'copy' => 'required',
            'represent' => 'required',
    		
    	],
    		[
			'title.required' => '网站标题不能为空',
            'keywords.required' => '网站关键字不能为空',
            'copy.required' => '网站版权不能为空',
            'represent.required' => '网站描述不能为空',  
			]
    	);
            // dd($data);
            $id = $request->input('id');
            $res = DB::table('config')->where('id',$id)->update($data);
            if($res){
                return redirect('/admin/config/index');
                // echo 233;
            }else{
                return back()->with('error','修改失败');
            }

       
    }
	  /**
      * 图片上传操作
      */
      public function upload(Request $request)
      {
          if($request->hasFile('logo')){
              //文件名称
              $name = md5(time()+rand(111,99999));
              //获取文件后缀名
              $suffix = $request->file('logo')->getClientOriginalExtension();
              $arr = ['jpg','png','gif','peng','jpeg'];
              //判断上传文件类型
              if(!in_array($suffix,$arr)){
                  echo '上传文件不符合要求';die;
              }
              //将指定文件移动到指定位置
              $request->file('logo')->move('./uploads/goods/',$name.'.'.$suffix);
              //将文件路径及文件名称返回
              return '/uploads/goods/'.$name.'.'.$suffix;
          }

          
      }
    //网站维护
    public function weihu()
    {
        // return 222;
        $data = DB::table('config') -> first();
        
         if($data -> status == 1)
        {
            return view('errors.503');
        }else{
            return redirect('/');
        }
    }
}
